package com.brozers.islamdini;


import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.CameraPosition;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.Marker;
import com.google.android.gms.maps.model.MarkerOptions;
import com.google.android.gms.maps.model.PolylineOptions;

import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.content.Context;
import android.graphics.Color;
import android.support.v4.app.FragmentActivity;

public class Map extends FragmentActivity implements LocationListener {
    GoogleMap               myMap;
    Location                lastLocation;
    private LocationManager locManager;
    private static LatLng MOUNTAIN_VIEW;
    Marker startPerc;
    String provider;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.map);
        myMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap();
        myMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);
        myMap.setMyLocationEnabled(true);
        
        
        locManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
        Criteria criteria = new Criteria();
        provider = locManager.getBestProvider(criteria, false);
        Location location = locManager.getLastKnownLocation(provider);
        locManager.requestLocationUpdates(provider, 20000, 1, this);
            if(location!=null){
            	if(startPerc != null){
            	startPerc.remove();
            	}
                onLocationChanged(location);
                
            }
          
        
        
    }

    @Override
    protected void onResume() {
        super.onResume();
        locManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, this);

    }
    @Override
    public void onLocationChanged(Location location) {
        if (location != null) {
        	MOUNTAIN_VIEW = new LatLng(location.getLatitude(),location.getLongitude());
        	
            lastLocation=locManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
            
            startPerc =myMap.addMarker(new MarkerOptions()
            .position(MOUNTAIN_VIEW)
            .icon(BitmapDescriptorFactory.fromResource(R.drawable.namazicon)));
            
            myMap.addPolyline(new PolylineOptions()
            .add(new LatLng(location.getLatitude(),location.getLongitude()), new LatLng(21.44796, 39.82613))
            .geodesic(true).color(Color.BLUE));
            
            CameraPosition cameraPosition = new CameraPosition.Builder()
            .target(MOUNTAIN_VIEW)      
            .zoom(17)                   
            .build();                  
            myMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
        }
    }

	@Override
	public void onProviderDisabled(String provider) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onProviderEnabled(String provider) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onStatusChanged(String provider, int status, Bundle extras) {
		// TODO Auto-generated method stub
		
	}

}
